package com.Mybatis3源码深度解析.c2;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.junit.Test;

public class DbMetaDataDemo {

	@Test
    public void testDbMetaData() {
        try {
        	Class.forName("com.mysql.jdbc.Driver");
        	Properties prop = Resources.getResourceAsProperties("hikariPool.properties");
            // 获取Connection对象
            Connection conn = DriverManager.getConnection(prop.getProperty("jdbcUrl"), prop.getProperty("dataSource.user"), prop.getProperty("dataSource.password"));
            DatabaseMetaData dmd = conn.getMetaData();
            System.out.println("数据库URL:" + dmd.getURL());
            System.out.println("数据库用户名:" + dmd.getUserName());
            System.out.println("数据库产品名:" + dmd.getDatabaseProductName());
            System.out.println("数据库产品版本:" + dmd.getDatabaseProductVersion());
            System.out.println("驱动主版本:" + dmd.getDriverMajorVersion());
            System.out.println("驱动副版本:" + dmd.getDriverMinorVersion());
            System.out.println("数据库供应商用于schema的首选术语:" + dmd.getSchemaTerm());
            System.out.println("数据库供应商用于catalog的首选术语:" + dmd.getCatalogTerm());
            System.out.println("数据库供应商用于procedure的首选术语:" + dmd.getProcedureTerm());
            System.out.println("null值是否高排序:" + dmd.nullsAreSortedHigh());
            System.out.println("null值是否低排序:" + dmd.nullsAreSortedLow());
            System.out.println("数据库是否将表存储在本地文件中:" + dmd.usesLocalFiles());
            System.out.println("数据库是否为每个表使用一个文件:" + dmd.usesLocalFilePerTable());
            System.out.println("数据库SQL关键字:" + dmd.getSQLKeywords());
            System.out.println("数据库表名允许的最大字符数:" + dmd.getMaxTableNameLength());
            System.out.println("==============获取数据源限制（以下方法返回值为int类型，当返回值为0时，表示没有限制或限制未知）==============");
            System.out.println("获取最大行数:" + dmd.getMaxRowSize());
            System.out.println("获取此数据库在sql语句中允许的最大字符数:" + dmd.getMaxStatementLength());
            System.out.println("获取此数据库在select语句中允许的最大表数:" + dmd.getMaxTablesInSelect());
            System.out.println("获取此数据库支持的最大连接数:" + dmd.getMaxConnections());
            System.out.println("获取此数据库表中允许的最大列数:" + dmd.getMaxColumnsInTable());

            System.out.println("==============以下方法返回ResultSet对象，该对象类型为TYPE_FORWARD_ONLY，并行性为CONCUR_READ_ONLY。可以调用ResultSet对象的getHoldability()方法获取可保持性==============");
            System.out.println("获取schema信息:" + dmd.getSchemas());
            System.out.println("获取catalog信息:" + dmd.getCatalogs());
            //System.out.println("获取表信息:" + dmd.getTables(catalog, schemaPattern, tableNamePattern, types));
            //System.out.println("获取主键信息:" + dmd.getPrimaryKeys(catalog, schema, table));
            //System.out.println("获取存储过程信息:" + dmd.getProcedureColumns(catalog, schemaPattern, procedureNamePattern, columnNamePattern));
            //System.out.println("获取用户自定义数据类型:" + dmd.getUDTs(catalog, schemaPattern, typeNamePattern, types));
            //System.out.println("获取函数信息:" + dmd.getFunctions(catalog, schemaPattern, functionNamePattern));
            //System.out.println("获取给定类别的函数参数和结果列的信息:" + dmd.getFunctionColumns(catalog, schemaPattern, functionNamePattern, columnNamePattern));

            System.out.println("==============获取事务支持==============");
            System.out.println("是否支持某一事务隔离级别:" + dmd.supportsTransactionIsolationLevel(1));
            System.out.println("是否支持事务:" + dmd.supportsTransactions());
            System.out.println("是否支持保存点:" + dmd.supportsSavepoints());
            System.out.println("获取默认的事务隔离级别:" + dmd.getDefaultTransactionIsolation());
            System.out.println("是否支持同时开启多个事务:" + dmd.supportsMultipleTransactions());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
